<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #663399;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-container {
            background-color: white;
            border: 2px solid #ddd;
            border-radius: 5px;
            padding: 20px;
            width: 350px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        fieldset {
            border: 2px solid #ddd;
            border-radius: 4px;
            padding: 20px;
            margin: 0;
        }
        legend {
            text-align: center;
            font-weight: bold;
            font-size: 1.5em;
            color: #663399;
            padding: 0 15px;
            background-color: white;
            border: 0px solid #ddd;
            border-radius: 4px;
        }
        .form-row {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
        }
        label {
            width: 80px;
            text-align: center;
            margin-right: 10px;
            font-weight: bold;
        }
        input[type="text"],
        input[type="password"] {
            flex: 1;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .button-group {
            display: flex;
            justify-content: center;
            margin-top: 20px;
        }
        button {
            padding: 8px 15px;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
            font-size: 15px;
            background-color: #f9f9f9;
        }
        button[type="submit"] {
            background-color: #f0f0f0;
        }
        button:hover {
            background-color: #e0e0e0;
        }
        .error {
            color: red;
            font-size: 0.8em;
            margin-top: 5px;
            margin-left: 90px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <form id="loginForm">
            <fieldset>
                <legend>用户登录</legend>
                <div class="form-row">
                    <label for="username">用户名:</label>
                    <input type="text" id="username" name="username">
                </div>
                <div id="usernameError" class="error"></div>

                <div class="form-row">
                    <label for="password">密码:</label>
                    <input type="password" id="password" name="password">
                </div>
                <div id="passwordError" class="error"></div>

                <div class="button-group">
                    <button type="submit">提交</button>
                    <button type="reset">重置</button>
                </div>
            </fieldset>
        </form>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();

            // 获取输入值
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value.trim();

            // 重置错误信息
            document.getElementById('usernameError').style.display = 'none';
            document.getElementById('passwordError').style.display = 'none';

            let isValid = true;

            // 验证用户名
            if (!username) {
                document.getElementById('usernameError').textContent = '用户名不能为空';
                document.getElementById('usernameError').style.display = 'block';
                document.getElementById('username').focus();
                isValid = false;
            } else if (username.length < 8 || username.length > 20) {
                document.getElementById('usernameError').textContent = '用户名长度必须为8-20个字符';
                document.getElementById('usernameError').style.display = 'block';
                document.getElementById('username').focus();
                isValid = false;
            }

            // 验证密码
            if (!password) {
                document.getElementById('passwordError').textContent = '密码不能为空';
                document.getElementById('passwordError').style.display = 'block';
                if (isValid) document.getElementById('password').focus();
                isValid = false;
            } else if (password.length < 8 || password.length > 20) {
                document.getElementById('passwordError').textContent = '密码长度必须为8-20个字符';
                document.getElementById('passwordError').style.display = 'block';
                if (isValid) document.getElementById('password').focus();
                isValid = false;
            }

            // 如果验证通过，可以提交表单
            if (isValid) {
                alert('登录信息验证通过！');
                // 这里可以添加实际的表单提交代码
                // this.submit();
            }
        });
    </script>
</body>
</html>